import axios from 'axios'
import * as d3 from 'd3';
import Plotly from "plotly.js-dist";
import config from '../../config'

config.apiUrl = config.apiUrl + (config.apiUrl.endsWith('/') ? '' : '/')

const getApi = async (api, params) => {
    return (await axios.get(config.apiUrl + api + window.location.search, {
        params: {
            data: JSON.stringify(params)
        }
    })).data.data
}

export const stuName = [
    '标准轨迹',
    '管笑',
    '江聪',
    '王文超',
    '吴郑轩',
    '向翔宇',
    '许陈长立'
]

export const color = ["#ca0401", "#d3fe14", "#147b31", "#a505c7", "#ffb1a8", "#33fcd6", "#886b6a", "#4753fe", "#b89702", "#fd3aa1", "#2c7e8f", "#df93fe", "#28c310", "#e3ecf8", "#8dac98", "#fd6e0f", "#8879b3", "#f0eaa8", "#7c640b", "#a93662", "#06c1ce", "#b7a6b8", "#d37274", "#ef13fe", "#39fe75", "#fec806", "#feccfa", "#b867a6", "#a9ca63", "#b45b1c", "#0e8ee3", "#b89773", "#576e5e", "#24cc89", "#739948", "#933dff", "#049e80", "#f68dc4", "#8ce9fe", "#7d99ae", "#fe374a", "#b4ddc2", "#f4a552", "#636779", "#c7bff9", "#c609a2", "#805786", "#818bff", "#aac2cd", "#ccc0ae", "#b868e9", "#c1ff9c", "#3f6ba6", "#fedfdf", "#83815a", "#918e8d", "#89e006", "#9c4f47", "#0c9c11", "#de0562", "#695ab4", "#ce8133", "#72cebc", "#678a85", "#b08292", "#ab98e4", "#667a01", "#017967", "#c6b364", "#019ebe", "#f98a69", "#ca4d3f", "#8eaf07", "#fe5edc", "#ad745a", "#8ae396", "#b9faf2", "#f7c694", "#dee264", "#cd5382", "#bd8bc3", "#855c39", "#538960", "#d6cada", "#a15e77"]

const APIHelper = {
    getTrajectoryXYZ: async () => {
        let trajectory_xyz = await getApi('trajectoryXYZ/', {all:true})
        let step = 698
        let n = trajectory_xyz.length / step

        let x = {}
        let y = {}
        let z = {}

        for(let i = 0; i < n; i++){
            if(!x.hasOwnProperty(trajectory_xyz[i * step]['stuid'])) {
                x[trajectory_xyz[i * step]['stuid']] = [[]]
                y[trajectory_xyz[i * step]['stuid']] = [[]]
                z[trajectory_xyz[i * step]['stuid']] = [[]]
            }
            else{
                x[trajectory_xyz[i * step]['stuid']].push([])
                y[trajectory_xyz[i * step]['stuid']].push([])
                z[trajectory_xyz[i * step]['stuid']].push([])
            }
        }

        for(let i = 0; i < step; i++){
            for(let j = 0; j < n; j++){
                x[trajectory_xyz[j * step]['stuid']][trajectory_xyz[j * step]['dataid'] - 1].push(trajectory_xyz[j * step + i]['x'])
                y[trajectory_xyz[j * step]['stuid']][trajectory_xyz[j * step]['dataid'] - 1].push(trajectory_xyz[j * step + i]['y'])
                z[trajectory_xyz[j * step]['stuid']][trajectory_xyz[j * step]['dataid'] - 1].push(trajectory_xyz[j * step + i]['z'])
            }
        }
        return {
            x:x,
            y:y,
            z:z
        }
    },
    getTrajectory: async () => {
        let trajectory = await getApi('trajectory/', {all: true})
        let step = 698
        let n = trajectory.length / step
        let points = []
        let errs = []

        for(let i = 0; i < 698; i ++){
            points.push([])
            for(let j = 0; j < n; j++){
                points[i].push([trajectory[i + j * step]['stuid'], trajectory[i + j * step]['dataid'], trajectory[i + j * step]['angle'], trajectory[i + j * step]['err']])
            }
            errs.push(trajectory[i]['radius'])
        }
        let maxErr = Math.max(...errs)

        let stuList = {}

        for(let p in points[0]){
            if(stuList.hasOwnProperty(stuName[parseInt(points[0][p][0])])){
                stuList[stuName[parseInt(points[0][p][0])]]++
            }
            else {
                stuList[stuName[parseInt(points[0][p][0])]] = 1
            }
        }

        let count = 1
        let stuStartIdx = {}
        for(let key in stuList){
            stuStartIdx[key] = count
            count += stuList[key]
        }

        return {
            allPoints: points,
            allErrs: errs,
            maxErr: maxErr,
            stuList: stuList,
            stuStartIdx: stuStartIdx
        }
    },
    getMockData: async () => {
        const res = await axios.post('/api/data')
        return res.data
    },
    getMockStaticData: async () => {
        const res = await axios.post('/api/StaticData')
        let StuListTmp = {}
        let data = res.data
        for(let i in data){
            if(data[i].group in StuListTmp){
                StuListTmp[data[i].group].push(
                    {
                        name: data[i].name + "-" + data[i].dataId,
                        group: data[i].group,
                        avatar: 'https://bpic.588ku.com/element_pic/01/37/09/22573c3a831082c.jpg',
                        time: data[i].time,
                        horizontalScore: data[i].horizontalScore,
                        verticalScore: data[i].verticalScore
                    })
            }
            else{
                StuListTmp[data[i].group] = []
                StuListTmp[data[i].group].push({
                    name: data[i].name + "-" + data[i].dataId,
                    group: data[i].group,
                    avatar: 'https://bpic.588ku.com/element_pic/01/37/09/22573c3a831082c.jpg',
                    time: data[i].time,
                    horizontalScore: data[i].horizontalScore,
                    verticalScore: data[i].verticalScore
                })
            }
        }

        let StuList = []
        for(let key in StuListTmp){
            StuList.push({header: key})
            StuList = StuList.concat(StuListTmp[key])
            StuList.push({ divider: true })
        }
        // console.log('StuList', StuList)
        return {
            StuList: StuList,
            Score: data
        }
    },
}

export default APIHelper